/**
 * 
 */
package org.mhz.ootw.util;

import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.mhz.ootw.tree.JsTreeNode;

/**
 * @author Mahan
 *
 * 
 */
public final class JSONHelper
{
    /**
     * Private constructor
     */
    private JSONHelper()
    {
        
    }
    
    /**
     * @param aListOfJsTreeNodes - The list of JsTreeNodes
     * @return The list of JsTreeNodes as a json string 
     */
    public static String jsTreeNodesAsJSONString(final List<JsTreeNode> aListOfJsTreeNodes)
    {
        final StringBuilder jsonString = new StringBuilder();
        
        jsonString.append("["); 
        if (CollectionUtils.isNotEmpty(aListOfJsTreeNodes))
        {        
            String delimiter = "";
            for (JsTreeNode jsTreeNode : aListOfJsTreeNodes)
            {
                if (jsTreeNode != null)
                {
                    // delimiter is initially blank
                    jsonString.append(delimiter);        
                    
                    jsonString.append(jsTreeNode.jsonString());
                    
                    // once a value has been set the delimiter becomes a ,
                    delimiter = ",";
                }
            }            
        }
        jsonString.append("]");
        
        return jsonString.toString();        
    }
}
